package cn.bopomo.dealer.base.action;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bopomo.dealer.base.common.CommonConstant;
import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.vo.OperatorDetail;

public class LoginAction extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		if (!this.isValidateCheckCode(request)){
			//error信息
			return mapping.findForward(this.F);
		}
		
		String loginName = request.getParameter("loginName");
		String loginPwd = request.getParameter("password");
		OperatorDetail operator = null;
		
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		operator = os.login(loginName,loginPwd);
		if (operator == null){
			//error信息
			return mapping.findForward(this.F);
		}
		
		operator.setLastLoginDate(new Date());
		os.modifyOperator(operator);
		
		request.getSession().setAttribute(CommonConstant.KEY_LOGIN_NAME,operator.getLoginName());
		request.getSession().setAttribute(CommonConstant.KEY_LOGIN_ID,operator.getId().toString());
		
		//权限查询
		Map authorizeMap = os.getAuthorize(operator.getAuthorize());
		if (authorizeMap != null)
			request.getSession().setAttribute(CommonConstant.KEY_AUTHORIZE_MAP,authorizeMap);
		
		//模块查询
		Map moduleMap = os.getModuleListByTag(operator.getAuthorize());
		if (moduleMap != null)
			request.getSession().setAttribute(CommonConstant.KEY_MODULE_MAP,moduleMap);
		
		return this.findSuccess(mapping);
	}
	
}
